package com.csp.mingyue.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csp.mingyue.common.core.vo.R;
import com.csp.mingyue.common.log.annotation.Log;
import com.csp.mingyue.common.log.enums.BusinessType;
import com.csp.mingyue.common.mybatis.page.PageCommon;
import com.csp.mingyue.system.api.dto.UserAddOrEditDto;
import com.csp.mingyue.system.api.dto.UserQueryDto;
import com.csp.mingyue.system.api.entity.SysUser;
import com.csp.mingyue.system.api.vo.LoginUser;
import com.csp.mingyue.system.api.vo.SysUserVo;
import com.csp.mingyue.system.api.vo.UserInfoVo;
import com.csp.mingyue.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户表 前端控制器
 *
 * @author Strive
 * @since 2023-06-06 03:15:20
 */
@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/sysUser")
@RequiredArgsConstructor
public class SysUserController {

	private final SysUserService sysUserService;

	@GetMapping("list")
	@SaCheckPermission("system:user:list")
	@Operation(summary = "获取所有用户信息")
	public R<IPage<SysUserVo>> getSysUsers(PageCommon page, UserQueryDto dto) {
		return R.ok(sysUserService.getSysUsers(page, dto));
	}

	@GetMapping("info/{username}")
	@Operation(summary = "通过用户名查询用户信息",
			parameters = { @Parameter(name = "username", description = "用户名", required = true) })
	public R<LoginUser> userInfo(@PathVariable String username) {
		return R.ok(sysUserService.getSysUserInfoByUsername(username));
	}

	@GetMapping("info")
	@Operation(summary = "获取当前登录用户全部信息")
	public R<UserInfoVo> userInfo() {
		return R.ok(sysUserService.getUserInfo());
	}

	@GetMapping("infoByPhone/{phone}")
	@Operation(summary = "通过手机号查询用户信息",
			parameters = { @Parameter(name = "phone", description = "手机号", required = true) })
	public R<LoginUser> userInfoByPhone(@PathVariable String phone) {
		return R.ok(sysUserService.getSysUserInfoByPhone(phone));
	}

	@GetMapping("infoByEmail/{email}")
	@Operation(summary = "通过邮箱查询用户信息", parameters = { @Parameter(name = "email", description = "邮箱", required = true) })
	public R<LoginUser> userInfoByEmail(@PathVariable String email) {
		return R.ok(sysUserService.getSysUserInfoByEmail(email));
	}

	@PostMapping("register")
	@Operation(summary = "用户注册")
	public R<Boolean> register(@RequestBody SysUser sysUser) {
		return R.ok(sysUserService.register(sysUser));
	}

	@PostMapping("addOrEdit")
	@Operation(summary = "添加或编辑用户")
	public R<Boolean> addOrEdit(@Valid @RequestBody UserAddOrEditDto dto) {
		return R.ok(sysUserService.addOrEdit(dto));
	}

	@DeleteMapping("{userId}")
	@Log(module = "用户管理", businessType = BusinessType.DELETE)
	@Operation(summary = "删除用户", parameters = { @Parameter(name = "userId", description = "用户ID", required = true) })
	public R<Boolean> delUser(@PathVariable Long userId) {
		return R.ok(sysUserService.delUser(userId));
	}

}
